/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.common.service.impl.SysrefcodeServiceImpl;
import com.sitechasia.webx.core.utils.context.ApplicationContext;

/**
 * 功能描述：系统参数表下拉框
 * @author 罗军林 创建日期：2008-5-9
 */
public class SysrefcodeSelectTag extends TagSupport {
	//以下为标签对应的属性，除code、showTip、selectValue为自定义属性外，其它均为select标签属性
	private String id;
	private String name;
	private String style;
	private String className;
	private String tabIndex;
	private String onchange;

	private String selectValue;
	private String code;
	private Boolean showTip;
	private String filterStr;

	@Override
	public int doStartTag() throws JspException {
		try {
			WebApplicationContext context = getContext();
			ISysrefcodeService sysrefcodeService=(ISysrefcodeService)context.getBean("sysrefcodeService");
			List list = getDataList(sysrefcodeService);
			generationSelectHTML(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * Function:<p>根据code和filterStr来判断调用哪个service方法</p>
	 * 			<p>如果filterStr为空或为空格则根据code进行查询
	 * @param sysrefcodeService
	 * @return
	 * @throws Exception
	 * Create author:罗军林
	 * Create on:2008-6-11
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private List getDataList(ISysrefcodeService sysrefcodeService) throws Exception {
		List list;
		if(this.code==null)throw new Exception("The 'code' attrib must be in! ");
		if(this.filterStr==null || this.filterStr.trim().equals("")){
			list=sysrefcodeService.querySysrefCodesByCode(this.code);
		}else{
			list=sysrefcodeService.querySysrefCodesByCode(this.code,this.filterStr);
		}
		return list;
	}

	/**
	 * Function:取得web上下文环境
	 * @return
	 * Create author:罗军林
	 * Create on:2008-5-9
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private WebApplicationContext getContext() {
		ServletContext servletContext= this.pageContext.getServletContext();
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		return context;
	}

	/**
	 * Function:根据list内容动态输出该HTML脚本。<br>
	 * 默认情况下会有“请选择...”选项,如果有默认选择值但在列表不存在，则会默认选择“请选择...”项;<br>
	 * 如果showTip=false即“请选择...”，如果有默认选择值但不存在，将会默认选择第一项;<br>
	 * 控件的value值为SysrefcodeDO的id,控件的option 的code值为SysrefCodeDO的value;<br>
	 * <p>生成的示例：
	 * 			<SELECT  id="participantType1" name="participantType" onchange="showChange();" style="width:110px">
	 * 				<OPTION VALUE="19" code="001" >个人</OPTION>
	 * 				<OPTION VALUE="20" code="002" >企业</OPTION>
	 * 			</SELECT>
	 * </p>
	 * @param list
	 * @throws Exception
	 */
	private void generationSelectHTML(List list) throws Exception {
		StringBuffer out=new StringBuffer();
		StringBuffer temp=new StringBuffer();
		boolean isBeSelected=false;//标识默认选择值是否存在

		out.append("<SELECT ");
		if(this.id!=null)			out.append(" id=\""+this.id+"\"");
		if(this.name!=null) 		out.append(" name=\""+this.name+"\"");
		else throw new Exception("The 'name' attrib not null! ");
		if(this.className!=null)	out.append(" class=\""+this.className+"\"");
		if(this.onchange!=null)		out.append(" onchange=\""+this.onchange+"\"");
		if(this.tabIndex!=null) 	out.append(" tabIndex=\""+this.tabIndex+"\"");
		if(this.style!=null) 		out.append(" style=\""+this.style+"\"");
		out.append(">\n");

		for( Iterator<SysrefcodeDO> item=list.iterator();item.hasNext(); ){
			SysrefcodeDO itemDO=item.next();
			if( null != itemDO.getName() && null != itemDO.getId() && String.valueOf(itemDO.getId()).equals(this.selectValue) ){
				isBeSelected=true;
				temp.append( "<OPTION VALUE=\""+itemDO.getId()+"\" code=" +itemDO.getValue()+"\" SELECTED>"+itemDO.getName()+"</OPTION>\n" );
			}else{
				temp.append( "<OPTION VALUE=\""+itemDO.getId()+"\" code=\"" +itemDO.getValue()+"\" >"+itemDO.getName()+"</OPTION>\n" );
			}
		}

		if(isBeSelected && (showTip==null || showTip)){
			out.append("<OPTION  value=\"\">请选择...</OPTION>\n");
		}else if(showTip==null || showTip){
			out.append("<OPTION SELECTED value=\"\">请选择...</OPTION>\n");
		}

		out.append(temp);
		out.append("</SELECT>\n");
		pageContext.getOut().write(out.toString());
	}

/******************************************************************************************************/
	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getSelectValue() {
		return selectValue;
	}

	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}

	public String getTabIndex() {
		return this.tabIndex;
	}

	public void setTabIndex(String tableIndex) {
		this.tabIndex = tableIndex;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public Boolean getShowTip() {
		return showTip;
	}

	public void setShowTip(Boolean showTip) {
		this.showTip = showTip;
	}

	public String getFilterStr() {
		return filterStr;
	}

	public void setFilterStr(String filterStr) {
		this.filterStr = filterStr;
	}

}
